﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ecobee.Net
{
	/// <summary>
	/// Revisions are UTC date/time stamps in the format: YYMMDDHHMMSS. However, due to possible time drift between the 
	/// API consumer, the server and thermostat, it is recommended that they are treated as a string, rather than as a 
	/// date/time stamp. The recommended method to test for revision changes is to simply do a string comparison on the 
	/// previous and current revision. If the strings match, nothing changed. Otherwise request the thermostat including 
	/// the relevant information which changed.
	/// </summary>
	public class Revision : ApiObject
	{
		/// <summary>
		/// The current revision identifier.
		/// </summary>
		public string Identifier { get; set; }

		/// <summary>
		/// The previous revision identifier. If this value is empty
		/// then the revision has not been updated.
		/// </summary>
		public string PreviousIdentifier { get; set; }

		/// <summary>
		/// Indicates if this revision has changed since the last update.
		/// </summary>
		public bool IsUpdated
		{
			get
			{
				return (!string.IsNullOrWhiteSpace(this.PreviousIdentifier) && (this.Identifier != this.PreviousIdentifier));
			}
		}

		/// <summary>
		/// Converts a Ecobee.Net.Revision item to a System.String value.
		/// </summary>
		/// <param name="item">The instance of a Ecobee.Net.Revision to convert.</param>
		/// <returns>A System.String value representing the converted Ecobee.Net.Revision object.</returns>
		public static implicit operator string(Revision item)
		{
			return item.Identifier;
		}

		/// <summary>
		/// Converts a System.String value to a Ecobee.Net.Revision object.
		/// </summary>
		/// <param name="value">A System.String value to convert to a Ecobee.Net.Revision object.</param>
		/// <returns>A Ecobee.Net.Revision object converted from a System.String.</returns>
		public static implicit operator Revision(string value)
		{
			return new Revision() { Identifier = value };
		}

		public void SetNewValue(string value)
		{
			this.PreviousIdentifier = this.Identifier;
			this.Identifier = value;
		}

		/// <summary>
		/// Returns a string that represents the current object
		/// that can be used for display purposes.
		/// </summary>
		/// <returns>A string that represents the current object.</returns>
		public override string ToString()
		{
			return string.Format("{0}, {1}", this.Identifier, this.IsUpdated);
		}

		protected override string OnUniqueKey()
		{
			return this.Identifier;
		}

		protected override string OnGetKeyValue(dynamic dataObject)
		{
			return dataObject.identifier;
		}
	}
}
